<template>
  <div class="login box">
    <img src="../assets/img/Loginbg.jpg" alt="">
    <div class="login">
      <div class="login-title">
        <img src="../assets/img/logo.svg" alt="">
        <p>帮助有志向的年轻人通过努力学习获得体面的工作和生活!</p>
      </div>
      <div class="login_box">
        <div class="title">
          <span :class="{active:user.login_type==0}" @click="user.login_type=0">密码登录</span>
          <span :class="{active:user.login_type==1}" @click="user.login_type=1">短信登录</span>
        </div>
        <div class="inp" v-if="user.login_type==0">
          <input v-model="user.account" type="text" placeholder="用户名 / 手机号码" class="user">
          <input v-model="user.password" type="password" class="pwd" placeholder="密码">
          <div id="geetest1"></div>
          <div class="rember">
            <label>
              <input type="checkbox" class="no" v-model="user.remember"/>
              <span>记住密码</span>
            </label>
            <p>忘记密码</p>
          </div>
          <button class="login_btn" @click="mul_login">登录</button>
          <p class="go_login">没有账号
            <router-link to="/register">立即注册</router-link>
          </p>
        </div>
        <div class="inp" v-show="user.login_type==1">
          <input v-model="user.mobile" type="text" placeholder="手机号码" class="user" @blur="check_mobile">
          <input v-model="user.code" type="text" class="code" placeholder="短信验证码">
          <el-button id="get_code" type="primary" @click="send_sms">{{ user.sms_interval }}</el-button>
          <button class="login_btn" @click="sms_login">登录</button>
          <p class="go_login">没有账号
            <router-link to="/register">立即注册</router-link>
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ElMessage} from 'element-plus'
import {reactive, ref} from "vue";
import {reqMulLogin, reqCheckMobile, reqSmsLogin, reqSendSms} from "../api/user.js";
import {definedUser} from "../store/user.js";
import {useRouter} from "vue-router";

let $router = useRouter()
let $store_user = definedUser()

const user = reactive({
  login_type: 0, // 登录方式，0，密码登录，1，短信登录
  account: "",  // 登录账号/手机号/邮箱
  mobile: "",
  password: "", // 登录密码
  remember: false, // 是否记住登录状态
  code: "", // 短信验证码
  sms_interval: '获取验证码', // 短信按钮提示
  is_send: false,

})

// 校验手机端口
const check_mobile = async () => {
  if (!user.mobile) return;
  if (!user.mobile.match(/^1[3-9][0-9]{9}$/)) {
    ElMessage({
      type: 'error',
      message: '手机号有误',
      duration: 1000,
      onClose: () => {
        user.mobile = '';
      }
    })
    return false;
  }
  // 发送请求，校验手机号是否存在，如果存在，设置 is_send.value = true；表示能发短信，能点击发送短信按钮
  // 如果手机号不存在，就不能点击发送短信按钮
  let res = await reqCheckMobile(user.mobile)
  if (res.is_exisit) {
    user.is_send = true
  } else {
    ElMessage({
      type: 'error',
      message: '未注册，请先去注册',
      duration: 1000,
      onClose: () => {
        user.mobile = '';
      }
    })
  }
}


// 异步发送短信
const send_sms = async () => {
  // 定时器
  if (!user.is_send) return;  // is_send 是 false,就不能发送短信
  user.is_send = false;    // 防止用户点击两次，发了两个短信
  let sms_interval_time = 60;
  user.sms_interval = "发送中...";
  let timer = setInterval(() => {
    if (sms_interval_time <= 1) {
      clearInterval(timer);
      user.sms_interval = "获取验证码";
      user.is_send = true; // 重新回复点击发送功能的条件
    } else {
      sms_interval_time -= 1;
      user.sms_interval = `${sms_interval_time}秒后再发`;
    }
  }, 1000);

  // 发送验证码
  await reqSendSms(user.mobile)
}


// 多方式登录
async function mul_login() {
  if (user.account && user.password) {
    let res = await reqMulLogin(user.account, user.password)

    // 正常成功，是100，来到这里 res:{"code"，"msg"，"username"，"token"，"icon"}
    // 1 把用户名和 token 和 icon 保存到本地 {cookie} -->pinia中
    // $cookie.set('token', res.token, '7d')
    // $cookie.set('username', res.username, '7d')
    // $cookie.set('icon', res.icon, '7d')

    $store_user.set_user({username: res.username, icon: res.icon, token: res.token})
    $router.push('/')
  } else {
    ElMessage({
      type: 'error',
      message: '用户名或密码不能为空'
    })
  }

}


// 短信登录
async function sms_login() {
  if (user.mobile && user.code) {
    let res = await reqSmsLogin(user.mobile, user.code)

    $store_user.set_user({username: res.username, icon: res.icon, token: res.token})
    $router.push('/')

  } else {
    ElMessage({
      type: 'error',
      message: '手机号或验证码不能为空'
    })
  }

}


</script>

<style scoped>
.box {
  width: 100%;
  height: 100%;
  position: relative;
  overflow: hidden;
}

.box img {
  width: 100%;
  min-height: 100%;
}

.box .login {
  position: absolute;
  width: 500px;
  height: 400px;
  left: 0;
  margin: auto;
  right: 0;
  bottom: 0;
  top: -438px;
}

.login-title {
  width: 100%;
  text-align: center;
}

.login-title img {
  width: 190px;
  height: auto;
}

.login-title p {
  font-size: 18px;
  color: #fff;
  letter-spacing: .29px;
  padding-top: 10px;
  padding-bottom: 50px;
}

.login_box {
  width: 400px;
  height: auto;
  background: #fff;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
  border-radius: 4px;
  margin: 0 auto;
  padding-bottom: 40px;
  padding-top: 50px;
}

.title {
  font-size: 20px;
  color: #9b9b9b;
  letter-spacing: .32px;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-around;
  padding: 0px 60px 0 60px;
  margin-bottom: 20px;
  cursor: pointer;
}

.title span.active {
  color: #4a4a4a;
  border-bottom: 2px solid #84cc39;
}

.inp {
  width: 350px;
  margin: 0 auto;
}

.inp .code {
  width: 220px;
  margin-right: 16px;
}

#get_code {
  margin-top: 6px;
}

.inp input {
  outline: 0;
  width: 100%;
  height: 45px;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  text-indent: 20px;
  font-size: 14px;
  background: #fff !important;
}

.inp input.user {
  margin-bottom: 16px;
}

.inp .rember {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  margin-top: 10px;
}

.inp .rember p:first-of-type {
  font-size: 12px;
  color: #4a4a4a;
  letter-spacing: .19px;
  margin-left: 22px;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  /*position: relative;*/
}

.inp .rember p:nth-of-type(2) {
  font-size: 14px;
  color: #9b9b9b;
  letter-spacing: .19px;
  cursor: pointer;
}

.inp .rember input {
  outline: 0;
  width: 30px;
  height: 45px;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  text-indent: 20px;
  font-size: 14px;
  background: #fff !important;
  vertical-align: middle;
  margin-right: 4px;
}

.inp .rember p span {
  display: inline-block;
  font-size: 12px;
  width: 100px;
}

.login_btn {
  cursor: pointer;
  width: 100%;
  height: 45px;
  background: #84cc39;
  border-radius: 5px;
  font-size: 16px;
  color: #fff;
  letter-spacing: .26px;
  margin-top: 30px;
  border: none;
  outline: none;
}

.inp .go_login {
  text-align: center;
  font-size: 14px;
  color: #9b9b9b;
  letter-spacing: .26px;
  padding-top: 20px;
}

.inp .go_login span {
  color: #84cc39;
  cursor: pointer;
}
</style>